package com.virgo.demo.support.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.utils.ICommonUtils;
import com.virgo.demo.support.converter.DictDataConverter;
import com.virgo.demo.support.converter.DictDataSelectConverter;
import com.virgo.demo.support.domain.DictDataDo;
import com.virgo.demo.support.domain.bo.DictDataBo;
import com.virgo.demo.support.domain.vo.DictDataSelectVo;
import com.virgo.demo.support.domain.vo.DictDataVo;
import com.virgo.demo.support.mapper.DictDataMapper;
import com.virgo.demo.support.service.DictDataService;
import com.virgo.redis.annotation.ICache;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictDataDo> implements DictDataService {

	private final DictDataMapper dictDataMapper;

	private final DictDataConverter dictDataConverter;

	private final DictDataSelectConverter dictDataSelectConverter;

	/**
	 * 编辑字典值
	 * @param dictDataBo DictDataBo
	 */
	@Override
	public void saveDictData(DictDataBo dictDataBo) {
		DictDataDo dictDataDo = new DictDataDo();
		BeanUtils.copyProperties(dictDataBo,dictDataDo);
		if (ObjectUtils.isEmpty(dictDataDo.getDictCode()) || dictDataDo.getDictCode().compareTo(0L) < 1) {
			save(dictDataDo);
			return;
		}
		updateById(dictDataDo);
	}

	/**
	 * 删除字典值
	 * @param ids String
	 */
	@Override
	public void deleteDictData(String ids) {
		List<Long> idList = Arrays.asList(ICommonUtils.stringToArray(ids, ","));
		removeByIds(idList);
	}

	/**
	 * 字典值列表
	 * @param request HttpServletRequest
	 * @return ListDataVo<DictDataVo>
	 */
	@Override
	public ListDataVo<DictDataVo> findList(HttpServletRequest request) {
		long currentPage = IRequestUtils.getLong(request,"page");
		long pageSize = IRequestUtils.getLong(request,"pageSize");
		currentPage = Math.max(currentPage,1);
		pageSize = Math.max(pageSize,10);

		LambdaQueryWrapper<DictDataDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(DictDataDo::getDictCode);

		String dictType = IRequestUtils.getString(request,"dictType","");
		if (ObjectUtils.isEmpty(dictType)) {
			return new ListDataVo<>(0L,0L,new ArrayList<>());
		}
		queryWrapper.eq(DictDataDo::getDictType,dictType);

		Page<DictDataDo> page = new Page<>(currentPage, pageSize);
		IPage<DictDataDo> iPage = dictDataMapper.selectPage(page, queryWrapper);
		return new ListDataVo<>(iPage.getTotal(),iPage.getPages(),dictDataConverter.convert(iPage.getRecords(), DictDataVo.class));
	}

	/**
	 * 获取字典值的下拉
	 * @param dictType String
	 * @return List<DictDataSelectVo>
	 */
	@Override
	@ICache(keyPrefix = "dict_data", matchValue = "#dictType", ttlMin = 300, ttlMax = 600)
	public List<DictDataSelectVo> dictDataList(String dictType) {
		LambdaQueryWrapper<DictDataDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(DictDataDo::getDictCode).orderByDesc(DictDataDo::getDictSort);
		queryWrapper.select(DictDataDo::getDictLabel,DictDataDo::getDictValue);
		List<DictDataDo> dictDataDos = dictDataMapper.selectList(queryWrapper);
		return dictDataSelectConverter.convert(dictDataDos,DictDataSelectVo.class);
	}
}